package com.junzhiit.nengji.shop.dao.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import com.bainaeco.framework.base.dao.AbstractBaseDAO;
import com.bainaeco.framework.common.jdbc.StatementParameter;
import com.bainaeco.framework.enums.IsDelete;
import com.bainaeco.framework.util.DateUtil;
import com.junzhiit.nengji.shop.bo.OrdersAndGoodsBO;
import com.junzhiit.nengji.shop.bo.OrdersDataHistoryBO;
import com.junzhiit.nengji.shop.dao.OrdersDataHistoryDAO;
import com.junzhiit.nengji.shop.model.OrdersDataHistory;
import com.junzhiit.nengji.shop.model.enums.Is;
import com.junzhiit.nengji.shop.model.enums.OrdersDataHistoryStatus;

@Repository
public class OrdersDataHistoryDAOImpl extends AbstractBaseDAO<OrdersDataHistory> implements OrdersDataHistoryDAO{

	@Override
	public List<OrdersDataHistoryBO> findAll(OrdersDataHistoryBO ordersDataHistoryBO, int pageNumber,int maxPageRow) {
		StringBuffer sql = new StringBuffer();
		
		sql.append("select o.*,s.name 'shop_name',c.coupons_name,c.introduce");
		sql.append(" from orders_data_history o inner join shop s on o.shop_id = s.id");
		sql.append(" left join user_coupons c on o.coupons_id = c.id");
		sql.append(" where o.status = ? and o.is_delete = ?");
		
		StatementParameter param = new StatementParameter();
		param.setInt(OrdersDataHistoryStatus.COMPLETE.toIntValue());
		param.setInt(Is.NO.toIntValue());
		
		if(StringUtils.isNotBlank(ordersDataHistoryBO.getStartTime())){
			sql.append(" and o.created_time >= ?");
			param.setDate(DateUtil.getDateStart(DateUtil.parseDate(ordersDataHistoryBO.getStartTime())));
		}
		
		if(StringUtils.isNotBlank(ordersDataHistoryBO.getEndTime())){
			sql.append(" and o.created_time <= ?");
			param.setDate(DateUtil.getDateEnd(DateUtil.parseDate(ordersDataHistoryBO.getEndTime())));
		}
		
		if(StringUtils.isNotBlank(ordersDataHistoryBO.getShopId())){
			sql.append(" and o.shop_id = ?");
			param.setString(ordersDataHistoryBO.getShopId());
		}
		if(StringUtils.isNotBlank(ordersDataHistoryBO.getOrderNumber())){
			sql.append(" and o.order_number like ?");
			param.setString("%"+ordersDataHistoryBO.getOrderNumber()+"%");
		}
		if(StringUtils.isNotBlank(ordersDataHistoryBO.getTransactionNumber())){
			sql.append(" and o.transaction_number like ?");
			param.setString("%"+ordersDataHistoryBO.getTransactionNumber()+"%");
		}
		sql.append(" order by o.created_time desc");

		return queryForPagingList(sql.toString(), OrdersDataHistoryBO.class, param, pageNumber, maxPageRow);
	}

	@Override
	public int getTotal(OrdersDataHistoryBO ordersDataHistoryBO) {
		StringBuffer sql = new StringBuffer();
		
		sql.append("select count(o.id)");
		sql.append(" from orders_data_history o inner join shop s on o.shop_id = s.id");
		sql.append(" left join user_coupons c on o.coupons_id = c.id");
		sql.append(" where o.status = ? and o.is_delete = ?");
		StatementParameter param = new StatementParameter();
		param.setInt(OrdersDataHistoryStatus.COMPLETE.toIntValue());
		param.setInt(Is.NO.toIntValue());
		
		if(StringUtils.isNotBlank(ordersDataHistoryBO.getStartTime())){
			sql.append(" and o.created_time >= ?");
			param.setDate(DateUtil.getDateStart(DateUtil.parseDate(ordersDataHistoryBO.getStartTime())));
		}
		
		if(StringUtils.isNotBlank(ordersDataHistoryBO.getEndTime())){
			sql.append(" and o.created_time <= ?");
			param.setDate(DateUtil.getDateEnd(DateUtil.parseDate(ordersDataHistoryBO.getEndTime())));
		}
		
		if(StringUtils.isNotBlank(ordersDataHistoryBO.getShopId())){
			sql.append(" and o.shop_id = ?");
			param.setString(ordersDataHistoryBO.getShopId());
		}
		if(StringUtils.isNotBlank(ordersDataHistoryBO.getOrderNumber())){
			sql.append(" and o.order_number like ?");
			param.setString("%"+ordersDataHistoryBO.getOrderNumber()+"%");
		}
		if(StringUtils.isNotBlank(ordersDataHistoryBO.getTransactionNumber())){
			sql.append(" and o.transaction_number like ?");
			param.setString("%"+ordersDataHistoryBO.getTransactionNumber()+"%");
		}
		
		sql.append(" order by o.created_time desc");
		
		return queryForInt(sql.toString(),param);
	}

	@Override
	public List<OrdersAndGoodsBO> findDataDatail(OrdersAndGoodsBO ordersAndGoodsBO) {
		StringBuffer sql = new StringBuffer();
		StatementParameter param = new StatementParameter();
		sql.append("select o.*,s.name 'shop_name',c.coupons_name,t.full_name 'type_name',");
		sql.append("g.id 'orders_goods_id',g.orders_data_history_id,g.goods_name,g.sku_content_name,g.number,g.price,");
		sql.append("g.coupons_money 'goods_coupons_money',g.is_open_rebate,g.profit,g.profit_money 'goods_profit_money',");
		sql.append("g.primary_benefit_name,g.primary_benefit_ration,g.primary_benefit_money,");
		sql.append("second_benefit_name,g.second_benefit_ration,g.second_benefit_money,g.shop_get_benefit_ration,");
		sql.append("g.shop_get_benefit_money,g.referrer_name,g.referrer_identity,g.referrer_ration,");
		sql.append("g.referrer_money 'goods_referrer_money',g.agency_benefit 'goods_agency_benefit',");
		sql.append("g.agency_account 'goods_agency_account',g.shop_money 'goods_shop_money',");
		sql.append("g.gross_margin 'goods_gross_margin',g.gross_margin_rate 'goods_gross_margin_rate'");
		sql.append(" from orders_data_history o");
		sql.append(" inner join goods_data_history g on o.id = g.orders_data_history_id and g.is_delete = ?");
		sql.append(" inner join shop s on o.shop_id = s.id");
		sql.append(" inner join type t on g.type_id = t.id");
		sql.append(" left join user_coupons c on o.coupons_id = c.id");
		sql.append(" where o.created_time >= ? and o.created_time <= ? and o.status = ? and o.is_delete = ?");
		param.setInt(Is.NO.toIntValue());
		param.setDate(DateUtil.getDateStart(DateUtil.parseDate(ordersAndGoodsBO.getStartTime())));
		param.setDate(DateUtil.getDateEnd(DateUtil.parseDate(ordersAndGoodsBO.getEndTime())));
		param.setInt(OrdersDataHistoryStatus.COMPLETE.toIntValue());
		param.setInt(Is.NO.toIntValue());
		
		if(StringUtils.isNotBlank(ordersAndGoodsBO.getShopId())){
			sql.append(" and o.shop_id = ?");
			param.setString(ordersAndGoodsBO.getShopId());
		}
		sql.append(" order by o.created_time desc");
		
		return super.queryForList(sql.toString(), OrdersAndGoodsBO.class, param);
	}	
	
	@Override
	public OrdersDataHistory findByOrderId(String orderId) {
		StringBuffer sql = new StringBuffer();
		sql.append("select * from orders_data_history where orders_id = ?");
		StatementParameter params = new StatementParameter();
		params.setString(orderId);
		return super.query(sql.toString(),OrdersDataHistory.class,params);
	}
	
	@Override
	public List<OrdersDataHistoryBO> find(OrdersDataHistoryBO ordersDataHistoryBO, int pageNumber, int maxPageRow) {
		StringBuffer sql = new StringBuffer();
		sql.append("select c.coupons_name,c.introduce,o.order_number,o.shop_order_number,o.transaction_number,o.real_money,o.price_difference,");
		sql.append("o.id,o.coupons_money,o.coupons_type,o.pay_money,o.postage,o.profit_money,o.shop_money,o.fee,o.pay_way");
		sql.append(" from orders_data_history o left join user_coupons c on o.coupons_id = c.id");
		sql.append(" where o.is_delete = ? and o.status = ?");
		StatementParameter params = new StatementParameter();
		params.setInt(IsDelete.NO.toIntValue());
		params.setInt(OrdersDataHistoryStatus.COMPLETE.toIntValue());
		if (StringUtils.isNotBlank(ordersDataHistoryBO.getStartTime())) {
			sql.append(" and o.created_time >= ?");
			params.setDate(DateUtil.getDateStart(DateUtil.parseDate(ordersDataHistoryBO.getStartTime())));
		}
		if (StringUtils.isNotBlank(ordersDataHistoryBO.getEndTime())) {
			sql.append(" and o.created_time < ?");
			params.setDate(DateUtil.getDateEnd(DateUtil.parseDate(ordersDataHistoryBO.getEndTime())));
		}
		if(StringUtils.isNotBlank(ordersDataHistoryBO.getShopId())){
			sql.append(" and o.shop_id = ?");
			params.setString(ordersDataHistoryBO.getShopId());
		}
		sql.append(" order by o.created_time desc");
		return super.queryForPagingList(sql.toString(), OrdersDataHistoryBO.class, params, pageNumber, maxPageRow);
	}

	@Override
	public int totalCount(OrdersDataHistoryBO ordersDataHistoryBO) {
		StringBuffer sql = new StringBuffer();
		sql.append("select count(o.id) from orders_data_history o left join user_coupons c on o.coupons_id = c.id");
		sql.append(" where o.is_delete = ? and o.status = ?");
		StatementParameter params = new StatementParameter();
		params.setInt(IsDelete.NO.toIntValue());
		params.setInt(OrdersDataHistoryStatus.COMPLETE.toIntValue());
		if (StringUtils.isNotBlank(ordersDataHistoryBO.getStartTime())) {
			sql.append(" and o.created_time >= ?");
			params.setDate(DateUtil.getDateStart(DateUtil.parseDate(ordersDataHistoryBO.getStartTime())));
		}
		if (StringUtils.isNotBlank(ordersDataHistoryBO.getEndTime())) {
			sql.append(" and o.created_time < ?");
			params.setDate(DateUtil.getDateEnd(DateUtil.parseDate(ordersDataHistoryBO.getEndTime())));
		}
		if(StringUtils.isNotBlank(ordersDataHistoryBO.getShopId())){
			sql.append(" and o.shop_id = ?");
			params.setString(ordersDataHistoryBO.getShopId());
		}
		sql.append(" order by o.created_time desc");
		return super.queryForInt(sql.toString(),params);
	}
}
